<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ include file="/common/common.jspf" %>
<html>
<head>
<title>My JSP 'MyJsp.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">

    <script type="text/javascript">
        $(function () {

            $('#addTable').form("disableValidation");//禁用验证

            var oldWin = parent.$("iframe[title='店内餐桌管理']").get(0).contentWindow;//返回ifram页面文档（window)
            var path =$('#tableName').val()+"."+$('#tablePicPath').combobox("getText");
            alert($('#tablePicPath').combobox("getText"));
            $('#addTable').form('load',{
                tableCompanyName:oldWin.$('#tableCompanyName').combobox("getText"),
                tableStoreName:oldWin.$('#tableStoreName').combobox("getText"),
                tableCreate:myformatter(new Date()),
                tableUpdate:myformatter(new Date()),
               // tablePic:pic()
            });
            //验证座位数，座位数>0
            $.extend($.fn.validatebox.defaults.rules, {
                seatNum: { //验证座位数
                    validator: function(value, param){
                        return /^[+|-]?([1-9]+\.[1-9]+)|[1-9]+$/.test(value);
                    },
                    message: '座位数要大于0且为整数，请重新输入'
                }
            });

        });

        function pic() {
            var a = $('#tableName').val();
            var b = ".";
            var c = $('#tablePicPath').combobox("getText");
            var path = a+b+c;
            $.ajax({
                type: "post",
                url:"${proPath}/storetable/addTable.action",
                data:{tablePic: path},
                dataType: "json"
            });
        }

        function checkName() {
            alert($('#tableName').val());
            $('#addTable').form("enableValidation");//启用验证
            //验证餐桌名称是否重复
            $('#addTable').form('submit',{
                url:"${proPath}/storetable/selectName.action",
                success: function (data) {
                    if(data != '[]'){
                        alert("当前餐桌名称已存在，请重新输入")
                    }
                    if(data == '[]'){
                        checkNum();
                    }
                }
            });
        }
        function checkNum(){
            //开始验证
           $('#addTable').form('submit',{
                url:'${proPath}/storetable/select.action',
                success:function(data){
                    if(data == '[]'){
                        //编号不存在则提交表格
                        addTable();
                        pic();
                    }
                    if(data != '[]'){
                        //编号存在
                        alert("当前餐桌编号已存在，请重新输入");
                        /*var obj = eval ("(" + data + ")");
                        //数据库的数据
                        var name = obj[0].tableName;
                        var num = obj[0].tableNum;
                        //alert(name+num);
                        //表格的数据
                        var tablename = $('#tableName').val()//$('#tableName').validatebox("getValue")
                        var tablenum = $('#tableNum').val()//$('#tableNum').validatebox("getValue")
                        //alert(tablename+tablenum);

                        if(num !=tablenum && name==tablename){
                            alert("当前餐桌名称已存在，请重新输入");
                            return ;
                        }
                        if (num == tablenum && name!=tablename){
                            alert("当前餐桌编号已存在，请重新输入");
                            return ;
                        }
                        if (num ==tablenum && name==tablename){
                            alert("当前餐桌编号和名称已存在，请重新输入");
                            return ;
                        }*/
                    }
                }
            });
           /* if($('#addTable').form("validate")){
                alert("添加成功！")
            }*/
        }

        function myformatter(date){
            var day = date.getDate() > 9 ? date.getDate() : "0" + date.getDate();
            var month = (date.getMonth() + 1) > 9 ? (date.getMonth() + 1) : "0"
                + (date.getMonth() + 1);
            var hor = date.getHours();
            var min = date.getMinutes();
            var sec = date.getSeconds();
            return date.getFullYear() + '-' + month + '-' + day+" "+hor+":"+min+":"+sec;
        }

        function addTable() {
            var oldWin = parent.$("iframe[title='店内餐桌管理']").get(0).contentWindow;//返回ifram页面文档（window)
            //提交表单
            $('#addTable').form('submit',{
                url: '${proPath}/storetable/addTable.action',
                //成功后，提示成功信息
                success:function(){
                    alert("添加成功");
                    // 关闭本页面
                    parent.$('#win').window('close');
                    //刷新原页面
                    oldWin.$('#storeTable').datagrid("reload");
                }
            });
        }
    </script>


<body>
<form id="addTable"  method="post">
    <div>
        <label >餐桌编号:</label>
        <input class="easyui-validatebox" type="text" id="tableNum" name="tableNum" data-options="required:true" />
    </div>
    <div>
        <label >餐桌名称:</label>
        <input class="easyui-validatebox" type="text" id="tableName" name="tableName" data-options="required:true" />
    </div>
    <div>
        <label >座位数:</label>
        <input class="easyui-validatebox" type="text" name="tableSeatNum" validType="seatNum" data-options="required:true" />
    </div>
    <div>
        <label >就餐形式:</label>
        <select id="cc" class="easyui-combobox" name="tableWays" style="width:200px;">
            <option>整桌</option>
            <option>拼桌</option>
        </select>
    </div>
    <div>
        <input class="easyui-validatebox" type="hidden" name="tableStatus" value="可用"/>
    </div>
    <div>
        <input class="easyui-validatebox" type="hidden" name="tableCompanyName" />
    </div>
    <div>
        <input class="easyui-validatebox" type="hidden" name="tableStoreName" />
    </div>
    <div>
        <input class="easyui-validatebox" type="hidden" name="tableCreate" />
    </div>
    <div>
        <input class="easyui-validatebox" type="hidden" name="tableUpdate" />
    </div>

    <div>
        <label >图片的宽度:</label>
        <input class="easyui-validatebox" type="text" name="tablePicWeight" data-options="required:true" />
    </div>
    <div>
        <label >图片的高度:</label>
        <input class="easyui-validatebox" type="text" name="tablePicHeight" data-options="required:true" />
    </div>
    <div>
        <label >图片的后缀名:</label>
        <select id="tablePicPath" class="easyui-combobox" name="tablePicPath" style="width:200px;">
            <option>gif</option>
            <option>bmp</option>
            <option>jpg</option>
        </select>
    </div>

    <div>
        <input class="easyui-validatebox" type="hidden" name="tablePic" />
    </div>


    <input type="button" onclick="checkName()" value="确定">
</form>
</body>
</html>